/*
 * Copyright (C) 2010 The Android Open Source Project 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package com.freeforall;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.ScrollView;
import android.app.Activity;

/**
 * The main activity for the Free for All application. Drives the game logic, controller and view.
 * @author Erik Buchholz
 * @author Mike Peterson
 */
public class MainActivity extends Activity implements OnClickListener{

	private Game game;
	private GameView gameView;
	private GameController gameController;
	private static Handler textHandler;
	private static Handler scrollHandler;
	private TextView textView;
	private ScrollView scrollView;
	private Object pauseObject;
	private boolean pausedOnLeave;
	
	public MainActivity(){
		
	}
	
	/** Called when the Activity is created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textView = (TextView)findViewById(R.id.textView);
        scrollView = (ScrollView)findViewById(R.id.scrollView);
        
        // Make a handler to change the text
        textHandler = new Handler(){
        	public void handleMessage(Message msg){
        		Bundle b = msg.getData();
        		textView.append("\n" + b.getString("eatData"));
        		MainActivity.scrollHandler.sendEmptyMessage(0);
        	}
        };
        
        scrollHandler = new Handler(){
        	public void handleMessage(Message msg){
        		scrollView.fullScroll(View.FOCUS_DOWN);
        	}
        };
        init();
        View btnPlus = findViewById(R.id.btnPlus);
        btnPlus.setOnClickListener(this);
        View btnMinus = findViewById(R.id.btnMinus);
        btnMinus.setOnClickListener(this);
        View chkPause = findViewById(R.id.chkPause);
        chkPause.setOnClickListener(this);
        View btnQuit = findViewById(R.id.btnQuit);
        btnQuit.setOnClickListener(this);
    }
    
    /** Pause threads on leaving activity. */
    @Override
    public void onStop(){
    	super.onStop();
    	pausedOnLeave = false;
    	if (((CheckBox)findViewById(R.id.chkPause)).isChecked()){
    		pausedOnLeave = true;
    	}
    	GameParams.getInstance().isPaused = true;
    	GameParams.getInstance().leaving = true;
    }
    
    /** Resume threads on resume. */
    @Override
    public void onResume(){
    	super.onResume();
    	if(GameParams.getInstance().leaving){
    		if(!pausedOnLeave){
        		GameParams.getInstance().isPaused = false;
        	}
    		GameParams.getInstance().leaving = false;
        	gameController.onReturn();
    	}
    }
    
    /** Finish off all threads. */
    @Override
    public void onDestroy(){
    	gameController.exit();
    	System.exit(0);
    }
    
    /** Set up the Activity when created. */
    private void init(){
    	pauseObject = new Object();
    	game = new Game(textHandler, pauseObject);
    	gameView = (GameView)findViewById(R.id.gameview);
    	gameView.init(game);
    	gameController = new GameController(game, gameView, pauseObject);
    	gameController.start();
    }
    
    /** Take the appropriate action for the button pressed. */
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btnPlus:
        	if(GameParams.getInstance().gameSpeed > 10){
        		GameParams.getInstance().gameSpeed -= 10;
        	}
            break;
        case R.id.btnMinus:
        	if(GameParams.getInstance().gameSpeed < 250){
        		GameParams.getInstance().gameSpeed += 10;
        	}
            break;
        case R.id.chkPause:
        	if (((CheckBox)findViewById(R.id.chkPause)).isChecked()){
        		GameParams.getInstance().isPaused = true;
        	}
        	else{
        		GameParams.getInstance().isPaused = false;
        	}
            break;
        case R.id.btnQuit:
        	gameController.exit();
        	System.exit(0);
            break;
        }
    }

    /*	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    */
}
